home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / CBBS60SO.ZIP / MBCONVM.C < prev    next >
Text File  |  1988-10-31  |  10KB  |  331 lines

  1. /*
  2.  *  MBCONVM - 10/31/88
  3.  *
  4.  *  Copyright (C) 1988
  5.  *  By the CBBS Group.
  6.  *
  7.  *  This program converts the mail textfiles from a version 8
  8.  *  mail system to version 9 format.
  9.  *  Version 9 format requires that the file header information
  10.  *  be included in the first record of the textfile.
  11.  *  Normal textfile data starts at offset 256 in the file.
  12.  *
  13.  *  It is expexted that CONVERTM.EXE be placed in the directory
  14.  *  containing the textfiles and be executed from there.
  15.  *  It is also expected that the file MAIL.DAT will be located in the
  16.  *  directory \MB\BBS but if this is not the case, the user will be
  17.  *  given the option of entering the actual directory path information.
  18.  *  Optionally the program can be invoked with the path to the mail.dat
  19.  *  as a command line parameter such as CONVERTM \HF\BBS  .
  20.  *  The converted textfiles will retain the original names and location.
  21.  *
  22.  */
  23.  
  24. #include "mb.h"
  25.  
  26. /*  Old message header record. */
  27.  
  28.  
  29. typedef struct omsg_hdr_s
  30. {
  31.   word next;          /* Next message header record                      */
  32.   word prev;          /* Previous message header record                  */
  33.   word ext;           /* Header extension record, or zero                */
  34.   word rn;            /* Record number of this record                    */
  35.   word read;          /* # times the message has been read               */
  36.   word number;        /* Message number                                  */
  37.   word size;          /* Size in bytes                                   */
  38.   char type;          /* Message type                                    */
  39.   byte stat;          /* Message status, see bit definitions above       */
  40.   char to  [ln_call]; /* Destination call                                */
  41.   char from[ln_call]; /* Originator call                                 */
  42.   char bbs [ln_call]; /* Destination BBS, or distribution list           */
  43.   char date[ln_date]; /* Entry date                                      */
  44.   char time[ln_time]; /* Entry time                                      */
  45.   char bid[ln_bid];   /* Bulletin ID, if this is a bulletin              */
  46.   char title[80];
  47.   char unu[120];
  48. } OMSG_HDR;
  49.  
  50. /*  Message header extension item. */
  51.  
  52. #define o_mmesn 35           /* Maxcalls i distribution list     */
  53.  
  54. typedef struct omsg_ext_s
  55. {
  56.   word next;                 /* Next extension item, or free item   */
  57.   char call[o_mmesn][ln_call]; /* Calls to send it to                 */
  58.   byte flag[o_mmesn];          /* TRUE if need to send, FALSE if sent */
  59.   byte count;                /* Number of calls in list            */
  60.   char unu[8];
  61. } OMSG_EXT;
  62.  
  63.  
  64. OMSG_HDR  *o_tmmhs;
  65. OMSG_EXT  *o_mmes;
  66.  
  67. /*
  68.  *  Old mail file header record.
  69.  */
  70.  
  71.  
  72. typedef struct omail_hdr_s
  73. {
  74.   word next;          /* Next record to allocate         */
  75.   word first;         /* First message header record     */
  76.   word last;          /* Last message header record      */
  77.   word ffree;         /* First record in free chain      */
  78.   word lfree;         /* Last record in free chain       */
  79.   word next_msg;      /* Next message number             */
  80.   word unt_msg;       /* next_msg at last untangle       */
  81.   byte version;       /* File format version number      */
  82.   word free;          /* Number of records in free chain */
  83.   word count;         /* Number of messages              */
  84.   char date[ln_date]; /* Date of last untangle           */
  85.   char time[ln_time]; /* Time of last untangle           */
  86.   char unu[227];
  87. } OMAIL_HDR;
  88.  
  89. OMAIL_HDR *o_mfhs;
  90.  
  91.  
  92.  
  93. main(argc, argv)
  94. int argc;
  95. char *argv[];
  96. {
  97.  
  98. #define chunk 4096
  99. register word h, rec;
  100. register char st, c;
  101. char *buf, *buf1, *mpath;
  102. char msgfile[10], tmpfile[10], mbfile[80], mbnfile[80];
  103. char flags[mmesn];
  104. int mfl, msgfl, msfl, mfln, n, first, do_it;
  105. MAIL_HDR  *mfhs;
  106. MSG_HDR   *tmmhs;
  107.  
  108.  
  109. /*
  110.  *  Allocate space for the mail file records.
  111.  */
  112.  
  113.   mfhs    =  (MAIL_HDR *) malloc(sizeof(MAIL_HDR));
  114.   tmmhs   =  (MSG_HDR *)  malloc(sizeof(MSG_HDR));
  115.   o_tmmhs =  (OMSG_HDR *) malloc(sizeof(OMSG_HDR));
  116.   o_mmes  =  (OMSG_EXT *) malloc(sizeof(OMSG_EXT));
  117.   o_mfhs  =  (OMAIL_HDR *)malloc(sizeof(OMAIL_HDR));
  118.  
  119.   buf     =  (char *) malloc(chunk);
  120.   buf1    =  (char *) malloc(RECSIZE);
  121.  
  122.   if(argc > 1) mpath = argv[1]; else mpath = "\\mb\\bbs";
  123.   sprintf(mbfile, "%s\\MAIL.DAT", mpath);
  124.  
  125. /*
  126.  *  Open the mail file.
  127.  */
  128.  
  129.   if ((mfl = open(mbfile, O_RDONLY | O_BINARY)) < 0)
  130.      { puts("MAIL.DAT is not in this directory.\nInput directory");
  131.        gets(mpath);
  132.        sprintf(mbfile, "%s\\MAIL.DAT", mpath);
  133.        if ((mfl = open(mbfile, O_RDONLY | O_BINARY)) < 0)
  134.          { puts("MAIL.DAT is not in that directory either. Aborting\n");
  135.            exit(1);
  136.          }
  137.      }
  138.  
  139. /*
  140.  *  Read the mail file header.
  141.  */
  142.  
  143.   read_rec(mfl, 0, (char *)mfhs);
  144.   if (mfhs->version is 9)
  145.    { puts("Mailfile has been converted previously. Aborting\n"); exit(1);}
  146.   read_rec(mfl, 0, (char *)o_mfhs);   
  147.   if (o_mfhs->version < 8 )
  148.    { puts("Mailfile is wrong version. Aborting\n"); exit(1);}
  149.  
  150. /*
  151.  *  Creat the NEW mail file
  152.  */
  153.  
  154.   sprintf(mbnfile, "%s\\MAIL.NEW", mpath);
  155.   if((mfln = open(mbnfile, O_CREAT | O_RDWR | O_BINARY, pmode)) < 0)
  156.    { puts("MAIL.NEW cannot be opened");
  157.      exit (1);
  158.    }
  159.   rec = 0;
  160. /*
  161.  *  Read the mail file records in sequence and
  162.  *  process each corresponding text file.
  163.  */
  164.  
  165.   for (h = o_mfhs->first; h; h = o_tmmhs->next)
  166.   {
  167.     read_rec(mfl, h, (char *)o_tmmhs);
  168.  
  169.     fill(tmmhs, '\0', 256);
  170.     fill(flags, ' ', mmesn);
  171.  
  172.     tmmhs->read     = o_tmmhs->read;
  173.     tmmhs->number   = o_tmmhs->number;
  174.     tmmhs->size     = o_tmmhs->size;
  175.     tmmhs->type     = o_tmmhs->type;
  176.     tmmhs->stat     = o_tmmhs->stat;
  177.     strncpy(tmmhs->to, o_tmmhs->to, ln_call);
  178.     strncpy(tmmhs->from, o_tmmhs->from, ln_call);
  179.     strncpy(tmmhs->bbs, o_tmmhs->bbs, ln_call);
  180.     strncpy(tmmhs->date, o_tmmhs->date, ln_date);
  181.     strncpy(tmmhs->time, o_tmmhs->time, ln_time);
  182.     strncpy(tmmhs->bid, o_tmmhs->bid, ln_bid);
  183.     strcpy(tmmhs->title, o_tmmhs->title);
  184.     tmmhs->ext = false;
  185.  
  186.     if (o_mmes->count > mmesn) o_mmes->count = mmesn;
  187.     if(o_tmmhs->ext)
  188.     {
  189.       tmmhs->ext = 0x01;
  190.       read_rec(mfl, o_tmmhs->ext, (char *)o_mmes);
  191.       for (c = 0; c < o_mmes->count; c++)
  192.       {
  193.           strncpy(tmmhs->call[c], o_mmes->call[c], ln_call);
  194.           tmmhs->flag[c] = o_mmes->flag[c];
  195.           flags[c] = tmmhs->flag[c] + '0';
  196.       }
  197.       tmmhs->count = o_mmes->count;
  198.     }
  199.  
  200.     sprintf(msgfile,"%u",o_tmmhs->number);
  201.     if ((msgfl = open(msgfile, O_RDONLY | O_BINARY)) < 0)
  202.         { printf("%5.5u textfile for this message not found\n", o_tmmhs->number);
  203.           continue;
  204.         }
  205.     
  206.     first = true;
  207.     do_it = true;
  208.     while ((n = read(msgfl, buf, chunk)) >0)
  209.     {
  210.       if(first)
  211.       {
  212.         if (n >= RECSIZE)
  213.         if ((buf[253] is '\0') and (buf[254] is '\r') and (buf[255] is '\n'))
  214.         {
  215.           printf("%5.5u was previously converted\n",
  216.              tmmhs->number);
  217.           do_it = false;
  218.           break;
  219.         }
  220.         printf("%5.5u %s\n", o_tmmhs->number, o_tmmhs->title);
  221.  
  222.     /*  Prepare and write the internal header to the temporary file */
  223.  
  224.         fill (buf1, '\0', 256);
  225.         buf1[254] = '\r'; buf1[255] = '\n';
  226.         st = 'N';
  227.         if (o_tmmhs->stat & m_stale) st = 'O';
  228.         if (o_tmmhs->stat & m_fwd)   st = 'F';
  229.         if (o_tmmhs->stat & m_hold)  st = 'H';
  230.         if (o_tmmhs->stat & m_read)  st = 'Y';
  231.         if (o_tmmhs->stat & m_kill)  st = 'K';
  232.       sprintf(buf1,
  233. "%5u %c%c %5u %6.6s %6.6s %6.6s %6.6s %4.4s %-12.12s %5u\r\n%c %16.16s\r\n%s\r\n",
  234.           o_tmmhs->number, o_tmmhs->type, st, o_tmmhs->size,
  235.           o_tmmhs->to, o_tmmhs->from, o_tmmhs->bbs,
  236.           o_tmmhs->date, o_tmmhs->time, o_tmmhs->bid, o_tmmhs->read,
  237.           tmmhs->ext + '0', flags, o_tmmhs->title);
  238.         sprintf(tmpfile,"%u.tmp",o_tmmhs->number);
  239.         msfl = open(tmpfile, O_CREAT | O_WRONLY | O_BINARY, pmode);
  240.         write_rec(msfl, 0, (char *)buf1);
  241.  
  242.         first = false;
  243.       }
  244.       write (msfl, buf, n);
  245.     }
  246.     close(msgfl);
  247.     if (do_it)
  248.     {
  249.       close(msfl);
  250.       unlink(msgfile);
  251.       rename(tmpfile, msgfile);
  252.       ++rec;
  253.       tmmhs->rn = rec;
  254.       write_rec(mfln, rec, (char *)tmmhs);
  255.     }
  256.  
  257.   }
  258.  
  259. /*
  260.  *  Write new version number to mailfile and close the file.
  261.  */
  262.  
  263.   printf("MAIL.DAT had %u messages, MAIL.NEW has %u messages.\n",
  264.      o_mfhs->count, rec);
  265.   mfhs->version = mb_version;
  266.   mfhs->count = mfhs->last = rec;
  267.   mfhs->first = 1;
  268.   mfhs->next = rec + 1;
  269.   mfhs->free = 0;
  270.   mfhs->next_msg = o_mfhs->next_msg;
  271.   mfhs->unt_msg = o_mfhs->unt_msg;
  272.   strncpy(mfhs->date, o_mfhs->date, ln_date);
  273.   strncpy(mfhs->time, o_mfhs->time, ln_time);
  274.   fill(mfhs->unu, '\0', mfhsunu);
  275.  
  276.   write_rec(mfln, 0, (char *)mfhs);
  277.   close(mfl);
  278.   unlink(mbfile);
  279.   printf("Deleting %s\n", mbfile);
  280.   close(mfln);
  281.   printf("Renameing %s to %s\n", mbnfile, mbfile);
  282.   rename(mbnfile, mbfile);
  283.  
  284. }
  285.  
  286. /*
  287.  *  Write random record.
  288.  */
  289.  
  290. write_rec(fid, rec, buffer)
  291. int fid;
  292. int rec;
  293. char buffer[];
  294. {
  295.   long lseek();
  296.   long offs;
  297.  
  298.   offs = (long)rec * (long)RECSIZE;
  299.   lseek(fid, offs, 0);
  300.   return (write(fid, buffer, RECSIZE) is RECSIZE);
  301. }
  302.  
  303. /*
  304.  *  Read random record.
  305.  */
  306.  
  307. read_rec(fid, rec, buffer)
  308. int fid;
  309. int rec;
  310. char buffer[];
  311. {
  312.   long lseek();
  313.   long offs;
  314.  
  315.   offs = (long)rec * (long)RECSIZE;
  316.   lseek(fid, offs, 0);
  317.   return (read(fid, buffer, RECSIZE) is RECSIZE);
  318. }
  319.  
  320. /*
  321.  *  Fill some memory with a character.
  322.  */
  323.  
  324. fill(adr, ch, len)
  325. char *adr;
  326. char ch;
  327. int len;
  328. {
  329.   while (len--) *adr++ = ch;
  330. }
  331.